-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.cpp
More file actions
35 lines (32 loc) · 817 Bytes
/
Solution.cpp
File metadata and controls
35 lines (32 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>
using namespace std;
void printPattern(int n) {
int num = 1;
for(int i = 1; i <= n; i++) {
// Print leading spaces
for(int space = 1; space <= n - i; space++) {
cout << " ";
}
// Print numbers
if(i % 2 == 1) { // Odd rows: left to right
for(int j = 1; j <= n; j++) {
cout << setw(2) << num++ << " ";
}
} else { // Even rows: right to left
int temp = num + n - 1;
for(int j = 1; j <= n; j++) {
cout << setw(2) << temp-- << " ";
}
num += n;
}
cout << endl;
}
}
int main() {
int n;
cout << "Enter the value of n: ";
cin >> n;
printPattern(n);
return 0;
}